package com.jeeplus.test.jlnu.timeConfigManage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.timeConfigManage.domain.TimeConfig;
import com.jeeplus.test.jlnu.timeConfigManage.service.TimeConfigService;
import com.jeeplus.test.tools.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "时间段校长名配置")
@Slf4j
@RestController
@RequestMapping("timeConfig")
public class TimeConfigController {

    @Autowired
    private TimeConfigService timeConfigService;


    @PostMapping("/save")
    public R saveConfig(@RequestBody TimeConfig timeConfig) {
        int count = timeConfigService.saveOrUpdate(timeConfig);
        if(count < 0){
            return R.error("9999","数据添加失败");
        }
        return R.suc("配置保存成功");
    }

    @GetMapping("/list")
    public R listConfigs(Date date) {
        List<TimeConfig> configs = timeConfigService.listByDate(date);
        return R.suc(configs);
    }


    @ApiOperation("毕业时间配置分页")
    @GetMapping("/page")
    public Page<TimeConfig> getTimeConfigPage(TimeConfig timeConfig, @RequestParam(defaultValue = "1") int pageNum,
                                                              @RequestParam(defaultValue = "10") int pageSize) {
        Page<TimeConfig> page = new Page<>(pageNum, pageSize);
        Page<TimeConfig> pageResult = timeConfigService.getTimeConfigPage(timeConfig, page);
        return pageResult;
    }


    @ApiOperation(value = "删除", notes = "访问此接口，返回操作结果")
    @GetMapping("del")
    public R del(@RequestParam int id) {
        boolean opResult = timeConfigService.deleteById(id);
        if (opResult) {
            return R.suc();
        } else {
            return R.error();
        }
    }

}
